package com.blackandwhitesensation.sice.presentation.backingBeans;

import com.blackandwhitesensation.sice.exceptions.*;
import com.blackandwhitesensation.sice.modelo.*;
import com.blackandwhitesensation.sice.modelo.dto.TipoDocumentoDTO;
import com.blackandwhitesensation.sice.presentation.businessDelegate.BusinessDelegatorView;
import com.blackandwhitesensation.sice.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.RowEditEvent;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;


/**
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura/
 *
 */
public class TipoDocumentoView {
    private InputText txtValorTipoDocumento;
    private InputText txtIdTipoDoc;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<TipoDocumentoDTO> data;
    private TipoDocumentoDTO selectedTipoDocumento;

    public TipoDocumentoView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            TipoDocumentoDTO tipoDocumentoDTO = (TipoDocumentoDTO) e.getObject();

            if (txtValorTipoDocumento == null) {
                txtValorTipoDocumento = new InputText();
            }

            txtValorTipoDocumento.setValue(tipoDocumentoDTO.getValorTipoDocumento());

            if (txtIdTipoDoc == null) {
                txtIdTipoDoc = new InputText();
            }

            txtIdTipoDoc.setValue(tipoDocumentoDTO.getIdTipoDoc());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtValorTipoDocumento != null) {
            txtValorTipoDocumento.setValue(null);
            txtValorTipoDocumento.setDisabled(true);
        }

        if (txtIdTipoDoc != null) {
            txtIdTipoDoc.setValue(null);
            txtIdTipoDoc.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        TipoDocumento entity = null;

        try {
            Long idTipoDoc = new Long(txtIdTipoDoc.getValue().toString());
            entity = BusinessDelegatorView.getTipoDocumento(idTipoDoc);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtValorTipoDocumento.setDisabled(false);
            txtIdTipoDoc.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtValorTipoDocumento.setValue(entity.getValorTipoDocumento());
            txtValorTipoDocumento.setDisabled(false);
            txtIdTipoDoc.setValue(entity.getIdTipoDoc());
            txtIdTipoDoc.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveTipoDocumento(FacesUtils.checkLong(
                    txtIdTipoDoc), FacesUtils.checkString(txtValorTipoDocumento));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteTipoDocumento(FacesUtils.checkLong(
                    txtIdTipoDoc));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateTipoDocumento(FacesUtils.checkLong(
                    txtIdTipoDoc), FacesUtils.checkString(txtValorTipoDocumento));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtIdTipoDoc == null) {
                txtIdTipoDoc = new InputText();
            }

            txtIdTipoDoc.setValue(selectedTipoDocumento.getIdTipoDoc());

            BusinessDelegatorView.deleteTipoDocumento(FacesUtils.checkLong(
                    txtIdTipoDoc));
            data.remove(selectedTipoDocumento);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Long idTipoDoc, String valorTipoDocumento)
        throws Exception {
        try {
            BusinessDelegatorView.updateTipoDocumento(idTipoDoc,
                valorTipoDocumento);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("TipoDocumentoView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtValorTipoDocumento() {
        return txtValorTipoDocumento;
    }

    public void setTxtValorTipoDocumento(InputText txtValorTipoDocumento) {
        this.txtValorTipoDocumento = txtValorTipoDocumento;
    }

    public InputText getTxtIdTipoDoc() {
        return txtIdTipoDoc;
    }

    public void setTxtIdTipoDoc(InputText txtIdTipoDoc) {
        this.txtIdTipoDoc = txtIdTipoDoc;
    }

    public List<TipoDocumentoDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataTipoDocumento();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<TipoDocumentoDTO> tipoDocumentoDTO) {
        this.data = tipoDocumentoDTO;
    }

    public TipoDocumentoDTO getSelectedTipoDocumento() {
        return selectedTipoDocumento;
    }

    public void setSelectedTipoDocumento(TipoDocumentoDTO tipoDocumento) {
        this.selectedTipoDocumento = tipoDocumento;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
